<?php
namespace app\index\controller;

use \think\Controller;
use \think\Request;
use \think\Session;
use think\Lang;
use app\common\controller\Api as commonApi;
use app\user\traits\controllers\TraitUser;

/**
 * 订单信息管理
 */
class Order extends commonApi
{
    use TraitUser;
    const SERVICE_FEE = 0;//服务费
    //前置操作
    protected $beforeActionList = [
        'checkLogin'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('user/Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect(url('user/login','','',''));
            exit;
        }
    }
    /**
     * [save 保存订单信息区分房车与租车订单]
     * @return [type] [description]
     */
    public function postSaveOrder()
    {
        //判断是否同意
        $Protocol=input('post.Protocol');
        if (empty($Protocol)) {
            return $this->error('请同意协议',$url = null,'',$wait=5);
        }
        //图片验证码
        $imgCode = input('post.imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($imgCode)) {
            return $this->error(lang('图片验证码错误'),$url = null,'',$wait=5);
        }
        $user_id=session('user.id');//登录人id
        $sdate = input('post.take_car_date'); 
        $edate = input('post.leave_car_date');
        $caravan_id = input('post.caravan_id');
        $camp_id =db('caravan')->where('id',$caravan_id)->value('services');
        //费用详情
        $cost = model('Orders')->getCost($caravan_id,$sdate,$edate);
        $post_order_info['travel_coin'] = $cost['intergral_total'];//花费旅行币
        $post_order_info['total_money'] = $cost['money_total'];//出租消费钱数
        $post_order_info['service_fee'] = self::SERVICE_FEE;//服务费
        //添加联系人
        $post_info['user_id']=$user_id;
        $post_info['name']=input('post.real_name');
        $post_info['phone']=input('post.phone');
        $post_info['email']=input('post.email');
        $post_info['type']=input('post.type');
        $post_info['type_number']=input('post.id_card');
        //增加必须的联系人
        if (empty(input('post.rent_contact_id/a'))) {
            return $this->error('请填写联系方式',$url = null,'',$wait=5);
        }
        //房车详情信息
        $post_order_info['rent_contact_id'] = input('post.rent_contact_id/a');
        $post_order_info['take_car_date']=input('post.take_car_date');//入住日期
        $post_order_info['leave_car_date']=input('post.leave_car_date');//离开日期
        $post_order_info['take_car_area']=input('post.take_car_area');//取车地点
        $post_order_info['leave_car_area']=input('post.leave_car_area');//还车地点
        $post_order_info['caravan_id']=input('post.caravan_id');//房车id
        $post_order_info['camp_id']=$camp_id;//营地id
        $post_order_info['rent_user_id'] = session('user.id');//预约人id
        $post_order_info['caravan_user_id'] =model('Caravans')->where('id',input('post.caravan_id'))->value('user_id');//车主
        //生成订单
        $result = model('Orders')->createOrders($post_order_info);
        if ($result) {
            $number = $result->number;
            $this->redirect("/order/show?number=".$number);
        }else{
            return $this->error(lang('订单失败'));
        }
    }
    /**
     * [getShow 中转界面]
     * @param  [type] $number [description]
     * @return [type]         [description]
     */
    public function getShow($number)
    {
        $number = input('number');
        $post_order_info = model('Orders')->where('number',$number)->find();
        $rent_contact_id = $post_order_info->rent_contact_id;
        if (!empty($rent_contact_id)) {
            foreach ($rent_contact_id as $k => $v) {
                $list = model('ContactInfos')->where('id',$v)->find();
                $contacts[] = $list;
            }
            $this->assign('contacts',$contacts);
        }

        return view('reserve/paychose',['post_order_info'=>$post_order_info]);
    }
    /**
     * [postAddLinkman 添加联系人]
     * @return [type] [description]
     */
    public function postAddLinkman(){
        $user_id['user_id'] = Request::instance()->user->id;
        $info['name'] = $this->request->param('name');$info['type_number'] = input('post.type_number');
        $info['email'] = input('post.email');$info['phone'] = input('post.phone');
        $re = $this->getCheckNull($info);
        if (!$re) {
           return json(['code'=>0]);
        }
        $re = model('ContactInfos')->create(array_merge($this->request->post(),$user_id),true);
        if($re){return json(['code'=>1]);}
    }
   /**
     * 支付订单
     * @return [type] [description]
     */
    public function getPayOrder()
    {
        $param = model('user/Orders')->payOrder(['number' => $this->request->param('order_id')]);
        if (is_bool($param) && $param) {
            $this->success(Lang::get('Order has been paid successfully'), url("/order/allorders",'','','user'));
        }else {
            $this->redirect(url('/pay/dopay', $param, true, 'api'));
        }
    }

    /**
     * [FunctionName 取消未付款订单]
     * @param string $value [description]
     */
    public function getCancelOrder($number)
    {
        return model('Orders')->cancelOrder($number);
    }
    public function getCaravanName($caravan_id)
    {
        return db('caravan')->where('id',$caravan_id)->value('name');
    }
    public function getCampName($camp_id)
    {
        return db('camps')->where('id',$camp_id)->value('name');
    }
    /**
     * [sendEmail 发送邮箱]
     * @param  [type] $emailAddress [目标邮箱]
     * @param  [type] $theme        [邮件主题]
     * @param  [type] $content      [邮件内容]
     * @return [type]               [description]
     */
    public function sendEmail($emailAddress,$theme,$content)
    {
        $mail = AliyunEmail::init(config('mail'))->send($emailAddress, $theme, $content);
        return $mail;
    }
    public function sendWhichUser($type,$order_number)
    {
        $theme = '双辉旅程网';
        $o_t = ['1'=>'出租','2'=>'交换'];
        $orderinfo  = model("Orders")->where('number',$order_number)->find();
        $xia_dan    = $orderinfo->created_at;
        $start_date = $orderinfo->take_car_date;
        $end_date   = $orderinfo->leave_car_date;
        //车主邮箱
        $car_email = db('users')->where('id',$orderinfo->caravan_user_id)->value('email');
        $content1  = <<<data
尊敬的会员，您的房车于$xia_dan被他人下单。$o_t[$type]日期是$start_date至$end_date。
data;
        $this->sendEmail($car_email,$theme,$content1);
        //下单人邮箱
        $user_email = db('users')->where('id',$orderinfo->rent_user_id)->value('email');
        $content2  = <<<data
尊敬的会员，您于$xia_dan下单并支付成功。$o_t[$type]日期是$start_date至$end_date,祝您旅途愉快。
data;
        $this->sendEmail($user_email,$theme,$content2);
    }
    /**
     * [getCheckNickName 检测空字段]
     * @return [type] []
     */
    public function getCheckNull($info_array)
    {
        foreach ($info_array as $key => $value) {
            if (empty($value)) {
                return false;
            }
        }
        return true;
    }
}

